$(document).ready(function(){
//	alert("hi");
	/*FOR DAMAGE:

IF DAMAGE EXCEEDS : 2% AND ACTUAL DAMAGE IS SAY 2.50%
THEN ABOVE CALCULATIONS IS 
BASIC 2% LESS ACTUAL 2.5% SO FIGURE IS 0.50%
SO CLAIM WILL BE DONE AS 0.50%/2 i.e 0.25%

FOR SAND/SILICA:
IF SAND/SILICA EXCEEDS: 2% AND ACTUAL SAND /SILICA IS SAY 2.50%
THEN ABOVE CALCULATIONS IS 
BASIC 2%- 2.5% ACTUAL SO FIGURES COEMS TO 0.50%
AND ACTUAL CLAIM WILL BE ALSO 0.50% AND IF SAND EXCEEDS 6% THAN CLAIM WILL BE: DOUBLE
i.e: anything above 6% sand will be directly added in claim
FOR EXAMPLE:
IF SAND SILICA IS FOR EXAMPLE 7.5%
THAN CALCULATION WILL BE
BASIC2%-7.5%= 5.5% PLUS AS 7.5% ECXEEDS 6% ADDITIONAL 1.5% WILL BE ADDED OVER ABOVE 5.5%
TOTAL CLAIM WILL BE : 5.5%+1.5% = 7%

FOR MOISTURE:
IF MOISTURE EXCEEDS 10% AND ACTUAL MOISTURE IS SAY 12.50%
THEN ABOVE CALCULATIONS IS 
BASIC 10%-12.50% THAN ACTUAL CLAIM IS 2.5%
AND IF MOISTURE EXCEEDS 15% THAN CLAIM WILL E DOUBLE i.e: anything above 15% will be directly added in claim
FOR EXAMPLE:
IF MOISTURE IS 16.50%
THAN CALCULATION WILL BE
BASIC 10%-16.50% = 6.5% PLUS AS 16.5% EXCEEDS 15% ADDITIONAL 1.5% WILL BE ADDED OVER AND ABOVE 6.5%
TOTAL CLAIM WILL BE : 6.5%+ 1.5%= 8%*/
	 
	
	
	$('#sandActual, #sandDiff, #priceSubSand, #moistureActual,#moistureDiff,#priceSubMoisture, #damageActual, #damageDiff, #priceSubDamage, #costUnload, #discount, #paidAdvance, #totalRebate').bind('keypress', function (e) {
        return (e.which != 8 && e.which != 0 && (e.which < 48 || e.which > 57) && e.which != 46) ? false : true;
    });
	
	function roundTo2(amt){
		amt = parseFloat(amt);
		var roundedVal=0;
		roundedVal = parseFloat(Math.round(amt*100))/100.0;
		return roundedVal;
	}
	
	 $('#calculate').bind('click',function(){
		// BenchMark values set by Admin
		 //alert('sdfghj');
		 var silica =0;
		 var damage=0;
		 var moisture=0;
		 silica = $('#sandActual').val();
	damage = $('#damageActual').val();
	 moisture = $('#moistureActual').val();
	console.log(silica)
	
	// Values from DB
	
	var benchmarkSilica = 2;
	var benchmarkMoisture = 10;
	var benchmarkDamage = 2;
	
	
	// Parameters calculations
	
	var silicadiff = silica-benchmarkSilica;
	if(silicadiff<0)
	{
		silicadiff=0;
	}
	console.log("val ===>"+silicadiff);
	if(silica>6)
	{
		var excessSilica=0;
		excessSilica = silica-6;
		silicadiff = silicadiff + excessSilica;
	}
	var damagediff =0;
	 damagediff = damage-benchmarkDamage;
		damagediff = damagediff/2;
	if(damagediff<0)
	{
		damagediff=0;
	}
	var moisturediff=0;
	moisturediff = moisture-benchmarkMoisture;
	if(moisture>15)
 	{
		var excessMoisture=0;
		 excessMoisture = moisture-15;
		moisturediff = moisturediff + excessMoisture;
	}
	if(moisturediff<0)
	{
		moisturediff=0;
	}
	$('#sandDiff').val(roundTo2(silicadiff));
	$('#moistureDiff').val(roundTo2(moisturediff));
	$('#damageDiff').val(roundTo2(damagediff));
	$('#sandDiff').attr('value',roundTo2(silicadiff));
	$('#moistureDiff').attr('value',roundTo2(moisturediff));
	$('#damageDiff').attr('value',roundTo2(damagediff));
	//Price Calculations
	var weight=0;
	var rate=0;
	var gross=0;
	weight = $('#weight').val();
	 rate = $('#rate').val();
	 gross = weight*rate;
	//alert("sdfgh"+$('#gross').val());
	$('#gross').val(gross);
	$('#gross').attr('value',gross);
	var priceSubSand=0;
	var priceSubMoisture = 0;
	var priceSubDamage=0;
	priceSubSand = (silicadiff*gross)/100;
	 priceSubMoisture = (moisturediff*gross)/100;
	 priceSubDamage = (damagediff*gross)/100;
	
	$('#priceSubSand').val(roundTo2(priceSubSand));
	$('#priceSubSand').attr('value',roundTo2(priceSubSand));
	
	$('#priceSubMoisture').val(roundTo2(priceSubMoisture));
	$('#priceSubDamage').val(roundTo2(priceSubDamage));
	
	
	$('#priceSubMoisture').attr('value',roundTo2(priceSubMoisture));
	$('#priceSubDamage').attr('value',roundTo2(priceSubDamage));
	
	// Other Calculations
	var unloading=0;
	var cashdisc=0;
	var advance = 0;
	var addition=0;
	unloading = $('#costUnload').val();
	 cashdisc = $('#discount').val();
	 advance = $('#paidAdvance').val();
	 
	 
	 
	 addition = + unloading+ +cashdisc+ +advance;
	console.log(addition);
	var totalRebate=0;
	totalRebate =  + priceSubSand + priceSubMoisture + priceSubDamage + roundTo2(unloading) +roundTo2(cashdisc) +roundTo2(advance);
	
	$('#totalRebate, #less').val(roundTo2(totalRebate));
	$('#totalRebate, #less').attr('value',roundTo2(totalRebate));
	
	var dalali=0;
	dalali = $('#addDalali').val();
	var totalR=0;
	totalR = roundTo2(gross) + roundTo2(dalali) - roundTo2(totalRebate);
	
	$('#totalR').val(roundTo2(totalR));
	$('#totalR').attr('value',roundTo2(totalR));
	 
	 var inputs = $('input');
	 
	 $.each(inputs,function(key,val){
		 $(this).attr('value',($(this).val()));
	 });
	 });
	 // href="mailto:?subject=Mail from Our Site&body"
	/* $('#btnemail').bind('click',function() {
	 
	 var content = $('table tbody').html();
	 
     //   $('#form').attr('action',
     //                  'mailto:?subject=Mail from Our Site');
			//	$('#form').submit();	   
   //     alert(1);
    });*/
	 
	//alert($('table tbody').html());

	
	 
});

	